﻿#include "WRWorkDomainsGuiPart.h"
#include "WRWorkDomainsGuiView.h"
#include "../WRSoft-Utility/Runtime/WRClass.h"
#include "../WRSoft-GuiApp-Working-Common/Runtime/WRWorkMainGuiPartActivator.h"

#include <QLabel>
#include <QHBoxLayout>

//表示工作域管理部件创建器。
class WRWorkDomainsGuiPartActivator : public WRWorkMainGuiPartActivator
{
public:
    //初始化工作域管理部件创建器。
    inline WRWorkDomainsGuiPartActivator(void)
    {
        QChar className[] = WRWorkDomainsGuiPartStrArr;
        WRClass::enroll(WRWorkDomainsGuiPartClass, QString(className, WRWorkDomainsGuiPartStrLen), this);
    }
    //释放当前对象所占用的相关资源。
    ~WRWorkDomainsGuiPartActivator(void)
    {
        //add code here
    }

    //创建工作域管理部件。
    WRWorkMainGuiPart* create(const QDomElement&)
    {
        return new WRWorkDomainsGuiPart();
    }
};
//唯一实例。
static WRWorkDomainsGuiPartActivator g_workDomainsGuiPartActivator;

//初始化界面部件。
WRWorkDomainsGuiPart::WRWorkDomainsGuiPart(void)
{
    m_widget = 0;
    m_exists = false;
}
//释放当前对象所占用的相关资源。
WRWorkDomainsGuiPart::~WRWorkDomainsGuiPart(void)
{
    //add code here
}

//获取推荐尺寸。
QSize WRWorkDomainsGuiPart::sizeHint() const
{
    return QSize(800, 600);
}

//初始部件。
void WRWorkDomainsGuiPart::init(const WRObjRef<WRWorkDaemon>& daemon)
{
    const WRArray<WRWorkDomain*>& domains = daemon->domains();
    if (domains.count())
    {
        WRWorkDomainsGuiView* guiView = new WRWorkDomainsGuiView(daemon);
        m_exists = true;
        m_widget = guiView;
    }
    else
    {
        QLabel* label = new QLabel(QStringLiteral("没有工作域！"));
        label->setAlignment(Qt::AlignCenter);
        m_widget = label;
    }
    QHBoxLayout* layout = new QHBoxLayout();
    layout->setContentsMargins(2, 2, 2, 2);
    layout->addWidget(m_widget);
    setLayout(layout);
}

//打开部件。
void WRWorkDomainsGuiPart::open(void)
{
    if (m_exists)
    {
        WRWorkDomainsGuiView* guiView = qobject_cast<WRWorkDomainsGuiView*>(m_widget);
        guiView->open();
    }
}
//关闭部件。
void WRWorkDomainsGuiPart::close(void)
{
    if (m_exists)
    {
        WRWorkDomainsGuiView* guiView = qobject_cast<WRWorkDomainsGuiView*>(m_widget);
        guiView->close();
    }
}
